import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import './assets/theme/theme-dark/index.css'
import VueRouter from 'vue-router'
import routes from './routes'
import moment from 'dayjs'

Vue.use(ElementUI)
Vue.use(VueRouter)

Object.defineProperty(Vue.prototype, 'UPLOADURL', { value: process.env.UPLOAD_URL });
Object.defineProperty(Vue.prototype, 'BASEURL', { value: process.env.BASE_URL });
Object.defineProperty(Vue.prototype, 'moment', { value: moment });
Object.defineProperty(Vue.prototype, 'getImageUrl', { value: getImageUrl });


function getImageUrl(url) {
    if (!url) {
        return ''
    }

    if (url.startsWith("//") || url.startsWith("http://") || url.startsWith("https://")) {
        return url
    }

    return process.env.UPLOAD_URL + url
}

const router = new VueRouter({
    mode: "history",
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        sessionStorage.removeItem('user');
        sessionStorage.removeItem('jwt');
    }
    let jwt = sessionStorage.getItem('jwt');
    if (!jwt && to.path != '/login') {
        next({ path: '/login' })
    } else {
        next()
    }
})

new Vue({
    router,
    // store,
    render: h => h(App)
}).$mount('#app')